package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.dao.UserdaoSCM;
import com.zr.model.User;
import com.zr.service.UserService;

/**
 * @author  shichongming
 * @E-mail: 
 * @date 创建时间：2017年6月28日 下午4:50:58 
 * @version 1.0 
 * @parameter  
 * @since  
 * @return  
 */

@Controller
@Scope(value="prototype")
public class UserActionSCM extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware{		
	private HttpServletRequest req;
	private HttpServletResponse resp;
	private Map<String, Object> session;
	//页面的账号值
	private String uname;
	private String upsw;
  public String getUname() {
		return uname;
	}
	public void setUname(String uname) {
		this.uname = uname;
	}	
	public String getUpsw() {
		return upsw;
	}
	public void setUpsw(String upsw) {
		this.upsw = upsw;
	}


	//属性注入
	@Resource
	private  UserService   userService;
	/**
	 * 账号验证
	 * 
	 * */
	public  void accountNumber(){
	
		int uid=userService.findUidByUname(uname);
		
		try {
			PrintWriter pw=resp.getWriter();
			
			if(uid>0){
				pw.write("1");
			}
			else{
				pw.write("0");
			}
			pw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}

	
	
	/**
	 * 登录验证
	 * @throws IOException 
	 * 
	 * */
	public  void login() throws IOException{
	
		User u=userService.findUidByUnameAndUpw(uname, upsw);
		session.put("user", u);
		int uid=u.getUid();	
		String ustate=u.getUstate();
		PrintWriter pw=resp.getWriter();
		if(uid>0&"未登录".equals(ustate)){
			userService.updateUserUstate(uid);
			pw.write("1");
		}else{							
			pw.write("0");
		}		
		pw.close();
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
@Override
public String execute() throws Exception {
	// TODO Auto-generated method stub
	return super.execute();
}

@Override
public void setServletResponse(HttpServletResponse arg0) {
	
	this.resp=arg0;
}

@Override
public void setServletRequest(HttpServletRequest arg0) {
	// TODO Auto-generated method stub
	this.req=arg0;
}

@Override
public void setSession(Map<String, Object> arg0) {
	// TODO Auto-generated method stub
	this.session=arg0;
}
	

	 
}
